package net.easyjava.third.letv;

import com.alibaba.fastjson.JSONObject;
import net.easyjava.third.letv.api.LetvApiClient;
import net.easyjava.third.letv.bean.LetvActivity;
import net.easyjava.third.letv.constants.LetvLiveConstants;
import net.easyjava.third.letv.util.LetvApiUtil;
import net.easyjava.tools.date.DateConstants;
import net.easyjava.tools.date.DateTools;
import org.apache.http.HttpResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by wxnacy on 16/6/28.
 */
public class LetvLiveClient {
    private static Logger logger = LoggerFactory.getLogger(LetvLiveClient.class);
    protected static LetvApiClient letvApiClient;
    private static String userUnique;
    private static String secretKey;
    /**
     * 构造单例
     */
    private volatile static LetvLiveClient singleton;
    private LetvLiveClient(final String userUnique, final String secretKey){
        this.userUnique = userUnique;
        this.secretKey = secretKey;
        init();

    }
    public static LetvLiveClient getInstance(final String userUnique,final String secretKey) {
        if (singleton == null) {
            synchronized (LetvLiveClient.class) {
                if (singleton == null) {
                    System.out.println("LetvLiveClient.getInstance");
                    singleton = new LetvLiveClient(userUnique,secretKey);
                }
            }
        }
        if(letvApiClient == null){
            init();
        }
        return singleton;
    }

    private static synchronized void init(){
        if (letvApiClient == null) {
            letvApiClient = LetvApiClient.getInstance();
            letvApiClient.setApiUrl(LetvLiveConstants.API_URL);// http://api.live.lecloud.com/execute");
            letvApiClient.setHttpPort(80);
            letvApiClient.setUserid(userUnique);
            letvApiClient.setSecret(secretKey);
            try {
                letvApiClient.initialize();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }




    public LetvActivity createActivity(String activityName, long startTime, long endTime, int liveNum,
                                           int neededPushAuth) throws IOException{
        Map<String, String> params = new HashMap<String, String>();
        String beginTime = DateTools.format(startTime, DateConstants.DATETIME_FORMAT);
        String endtime = DateTools.format(endTime, DateConstants.DATETIME_FORMAT);
        LetvActivity letvActivity = new LetvActivity(activityName, beginTime, endtime, liveNum);
        params.put("activityName", letvActivity.getActivityName());
        params.put("startTime", letvActivity.getStartTime());
        params.put("endTime", letvActivity.getEndTime());
        params.put("liveNum", letvActivity.getLiveNum() + "");
        params.put("codeRateTypes", letvActivity.getCodeRateTypes());
        params.put("needRecord", letvActivity.getNeedRecord() + "");
        params.put("needTimeShift", letvActivity.getNeedTimeShift() + "");
        params.put("needFullView", letvActivity.getNeedFullView() + "");
        params.put("activityCategory", letvActivity.getActivityCategory());
        params.put("playMode", letvActivity.getPlayMode() + "");
        LetvActivity letvActivity1 = null;
        HttpResponse response = letvApiClient.executePost(LetvLiveConstants.LIVE_CREATE, LetvLiveConstants.VER, params);
        System.out.println("LetvApiUtil.getResponseBody(response) = " + LetvApiUtil.getResponseBody(response));
        if (response.getStatusLine().getStatusCode() != 200) {
            System.out.println(response.getStatusLine().getStatusCode());
            System.out.println(LetvApiUtil.getResponseBody(response));
        } else {
            String rtmpcontent = LetvApiUtil.getResponseBody(response);
            JSONObject result = JSONObject.parseObject(rtmpcontent);
            String activityId = result.getString("activityId");

            Map<String, String> params1 = new HashMap<String, String>();
            params1.put("activityId", activityId);
            params1.put("neededPushAuth", "0");
            params1.put("needIpWhiteList", "0");
            params1.put("needPlayerDomainWhiteList", "0");
            try {
                HttpResponse response1 = letvApiClient.executePost("letv.cloudlive.activity.sercurity.config", LetvLiveConstants.VER,
                        params1);
                if (response1.getStatusLine().getStatusCode() != 200) {
                    System.out.println(response1.getStatusLine().getStatusCode());
                    System.out.println(LetvApiUtil.getResponseBody(response1));
                }
            } catch (IOException e) {
                throw new IOException(e);
            }

            letvActivity.setActivityId(activityId);
            // letvActivity1.setActivityId(activityId);
            // if(neededPushAuth ==0){
            // letvActivity.setActivityId(activityId);
            // letvActivity.setNeededPushAuth(neededPushAuth);
            // letvActivity1 = updateActivityConfig(letvActivity);
            // }else{
            // letvActivity1 = getActivity(activityId);
            // }
            letvActivity1 = null;// getActivity(activityId);
        }
        return letvActivity1;
    }



    public static void main(String[] args) {

    }



}
